package com.david.mydemo.framework.config;


import com.david.mydemo.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Component
public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String requestURI = request.getRequestURI();
        String queryString = request.getQueryString();
        // swagger-ui.html 页面放行
        if (StringUtils.contains(requestURI, "/swagger-ui")
                || StringUtils.contains(requestURI, "/webjars/springfox-swagger-ui")
                || StringUtils.contains(requestURI, "/swagger-resources")) {
            if (StringUtils.isNotEmpty(queryString) && (queryString.contains("=http://") || queryString.contains("=https://"))) {
                log.error("漏洞处理，非法请求参数，请求地址：{}，请求参数：{}", requestURI, queryString);
                return false;
            }
            log.error("请求地址：{}", requestURI);
        }
        return true;
    }

}
